package team.you.change.util;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;
import team.you.change.dao.RoomMapper;
import team.you.change.pojo.Room;

import java.util.Random;
import java.util.UUID;

/**
 * 随机生成教室数据
 */
@RunWith(SpringRunner.class)
@ContextConfiguration({"classpath*:mapper/*.xml","classpath:spring-mybatis.xml"})
public class RoomRandomCreate {
    private String[] arr = {"一","二","三","四","五","六"};
    @Autowired
    private RoomMapper roomMapper;
    private static final Logger LOGGER = LogManager.getRootLogger();
    @Test
    public void create(){
        for (int i = 0; i < 100; i++){
            Room room = new Room();
            Random random = new Random();
            String uuid = UUID.randomUUID().toString();
            room.setId(uuid);
            room.setRoomCode(getCode());
            room.setSite(getSite(room.getRoomCode()));
            room.setRoomNumber(random.nextInt(90) + 10);
            room.setRoomType(random.nextInt(5) + 1);
            room.setOpenTime(random.nextInt(3) + 1);
            room.setState(random.nextInt(2));
            try {
                int insert = roomMapper.insert(room);
                if(insert > 0){
                    LOGGER.info("插入成功");
                }else {
                    LOGGER.info("插入失败");
                }
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }

    /**
     * 随机生成一个教室代码
     * @return 教室代码
     */
    private String getCode(){
        Random random = new Random();
        int one = random.nextInt(6) + 1;
        int two = random.nextInt(6) + 1;
        int three = random.nextInt(10) + 1;
        return one +"-"+ two +"0"+ three;
    }

    /**
     * 根据教室代码生成教室地址
     * @param code 教室代码
     * @return 教室地址
     */
    private String getSite(String code){
        Random random = new Random();
        int one = Integer.parseInt(code.substring(0, 1)) - 1;
        int two = Integer.parseInt(code.substring(2, 3)) - 1;
        int three = Integer.parseInt(code.substring(4, 5));
        String str1 = "";
        String str2 = "";
        for (int i = 0; i < arr.length; i++){
            if(one == i)
                str1 = arr[i];
            if(two == i)
                str2 = arr[i];
        }
        return "第"+ str1 +"教学楼"+ str2 +"楼"+ three +"号教室";
    }
}
